import javax.swing.*;     
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.lang.Object.*;
import java.io.File;
import javax.imageio.ImageIO;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class random_test extends JFrame implements ActionListener {
	
	BufferedImage neg_img, que_img, pos_img;	
	JLabel freqL, runsL,crapsL, sumL, chiL;
	JTextField fileField, errorField;
	JButton analyzeButton,browse,report;
	final JFileChooser fc = new JFileChooser();
	strop s;
	
	public random_test() {
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == browse)
		{
			int returnVal = fc.showOpenDialog(random_test.this);
			if(returnVal == JFileChooser.APPROVE_OPTION) {
				fileField.setText(fc.getSelectedFile().getPath());
			}
		}
		if(e.getSource() == analyzeButton)
		{
			s = new strop();
			s.load_txt(fileField.getText());
			s.get_alphabet();
			if (s.freq_test(Integer.parseInt(errorField.getText()))== true)
				freqL.setIcon(new ImageIcon(pos_img));
			else
				freqL.setIcon(new ImageIcon(neg_img));
			if(s.runs_test() == true)
				runsL.setIcon(new ImageIcon(pos_img));
			else
				runsL.setIcon(new ImageIcon(neg_img));
			if(s.craps_test(Integer.parseInt(errorField.getText())) == true)
				crapsL.setIcon(new ImageIcon(pos_img));
			else
				crapsL.setIcon(new ImageIcon(neg_img));
			if(s.sums_test(Integer.parseInt(errorField.getText())))
				sumL.setIcon(new ImageIcon(pos_img));
			else
				sumL.setIcon(new ImageIcon(neg_img));
			if(s.chi_test(Integer.parseInt(errorField.getText())))
				chiL.setIcon(new ImageIcon(pos_img));
			else
				sumL.setIcon(new ImageIcon(neg_img));
		}
	}
	
	private void add_constraint(GridBagConstraints c ,int x, int y)
	{
		c.gridx = x;
		c.gridy = y;
	}
	
    public void initGUI(final Container pane) {
    	//BufferedImage myPicture = ImageIO.read(new File("path-to-file"));
    	//JLabel picLabel = new JLabel(new ImageIcon( myPicture ));
        JPanel panel = new JPanel();
    	panel.setLayout(new GridBagLayout());
    	GridBagConstraints c = new GridBagConstraints();
        //panel.setLayout(new GridLayout(8, 4, 5, 5));
        //panel.setLayout(new FlowLayout(FlowLayout.CENTER));
        //panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    	c.fill = GridBagConstraints.VERTICAL;
        this.add_constraint(c, 0, 0);
        panel.add(new JLabel("Frequency Test"));
        this.add_constraint(c, 0, 1);
        panel.add(new JLabel("Runs Test "),c);
        this.add_constraint(c, 0, 2);
        panel.add(new JLabel("Sums Test"),c);
        this.add_constraint(c, 0, 3);
        panel.add(new JLabel("Chi Test"),c);
        this.add_constraint(c, 0, 4);
        panel.add(new JLabel("Craps Test"),c);
        this.add_constraint(c, 0, 5);
        panel.add(new JLabel("Enter error (percentage)"),c);
        this.add_constraint(c, 0, 6);
        fileField = new JTextField(10);
        panel.add(fileField,c);
        this.add_constraint(c, 0, 7);
        analyzeButton = new JButton("Analyze");
        panel.add(analyzeButton,c);
    	try {
    		File neg_file = new File("neg.jpg");
    		File que_file = new File("que.bmp");
    		File pos_file = new File("pos.jpg");
    		neg_img = ImageIO.read(neg_file);
    		que_img = ImageIO.read(que_file);
    		pos_img = ImageIO.read(pos_file);
            freqL = new JLabel(new ImageIcon(que_img));
            runsL = new JLabel(new ImageIcon(que_img));
            sumL = new JLabel(new ImageIcon(que_img));
            chiL = new JLabel(new ImageIcon(que_img));
            crapsL = new JLabel(new ImageIcon(que_img));
            this.add_constraint(c, 1, 0);
            panel.add( freqL,c );
            this.add_constraint(c, 1, 1);
            panel.add( runsL,c);
            this.add_constraint(c, 1, 2);
            panel.add( sumL, c);
            this.add_constraint(c, 1, 3);
            panel.add(chiL,c);
            this.add_constraint(c, 1, 4);
            panel.add(crapsL,c);
    	} catch(Exception e) {
    		System.out.println("error");
    	}
    	errorField = new JTextField(2);
    	this.add_constraint(c,1,5);
    	panel.add(errorField,c);
    	browse = new JButton("Browse");
    	this.add_constraint(c,1,6);
    	panel.add(browse,c);
    	report = new JButton("Report");
    	panel.add(report,c);
        //panel.add(new JImage());
        panel.setVisible(true);
        pane.add(panel);
        browse.addActionListener(this);
        analyzeButton.addActionListener(this);
    }
    
    private static void showGUI()
    {
        random_test frame = new random_test();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.initGUI(frame.getContentPane());
        frame.pack();
        frame.setVisible(true);
    }
 
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                showGUI();
            }
        });
    }
}